﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.WinForms;
using RM2012Server.Controller;

namespace RM2012Server.View
{
    public partial class frmServe : Office2007Form
    {
        #region Fields
        AreaCtrl m_AreaCtrl = new AreaCtrl();
        TablesCtrl m_TablesCtrl = new TablesCtrl();
        CategoryCtrl m_CategoryCtrl = new CategoryCtrl();
        MenuCtrl m_MenuCtrl = new MenuCtrl();
        #endregion

        #region Constructor
        public frmServe()
        {
            InitializeComponent();
        }
        #endregion
        
        #region Load
        private void frmServe_Load(object sender, EventArgs e)
        {
            LoadArea();
            if (listArea.Items.Count > 0)
            {
                listArea.Items[0].Selected = true;
                if (listTables.Items.Count > 0)
                {
                    listTables.Items[0].Selected = true;
                }
            }

            LoadCategory();
            if (listCategory.Items.Count > 0)
            {
                listCategory.Items[0].Selected = true;
                if (listMenu.Items.Count > 0)
                {
                    listMenu.Items[0].Selected = true;
                }
            }
        }
        #endregion
        

        private void LoadArea()
        {
            listArea.Items.Clear();
            DataTable dtArea = new DataTable();
            dtArea = m_AreaCtrl.GetAll();
            if (dtArea.Rows.Count > 0)
            {
                for (int i = 0; i < dtArea.Rows.Count; i++)
                {
                    ListViewItem item = new ListViewItem(dtArea.Rows[i]["AreaName"].ToString(), 0);
                    listArea.Items.Add(item);
                }
            }
        }

        private String GetAreaIdByAreaName(String areaName)
        {
            String areaId = String.Empty;
            areaId = m_AreaCtrl.GetIdByName(areaName);
            return areaId;
        }

        private void LoadTablesByAreaId(String areaId)
        {
            listTables.Items.Clear();
            DataTable dtTables = new DataTable();
            dtTables = m_TablesCtrl.GetByArea(areaId);
            if (dtTables.Rows.Count > 0)
            {
                for (int i = 0; i < dtTables.Rows.Count; i++)
                {
                    ListViewItem item = new ListViewItem(dtTables.Rows[i]["TablesName"].ToString(), 1);
                    if (dtTables.Rows[i]["StateId"].ToString().Equals("2"))
                    {
                        item.ForeColor = System.Drawing.Color.Red;
                    }
                    else if (dtTables.Rows[i]["StateId"].ToString().Equals("3"))
                    {
                        item.ForeColor = System.Drawing.Color.Green;
                    }
                    else if (dtTables.Rows[i]["StateId"].ToString().Equals("4"))
                    {
                        item.ForeColor = System.Drawing.Color.Gray;
                    }
                    listTables.Items.Add(item);
                }
            }
        }

        private void LoadTablesByAreaName(String areaName)
        {
            listTables.Items.Clear();
            String areaId = GetAreaIdByAreaName(areaName);
            DataTable dtTables = new DataTable();
            dtTables = m_TablesCtrl.GetByArea(areaId);
            if (dtTables.Rows.Count > 0)
            {
                for (int i = 0; i < dtTables.Rows.Count; i++)
                {
                    ListViewItem item = new ListViewItem(dtTables.Rows[i]["TablesName"].ToString(), 1);
                    if (dtTables.Rows[i]["StateId"].ToString().Equals("2"))
                    {
                        item.ForeColor = System.Drawing.Color.Red;
                    }
                    else if (dtTables.Rows[i]["StateId"].ToString().Equals("3"))
                    {
                        item.ForeColor = System.Drawing.Color.Green;
                    }
                    else if (dtTables.Rows[i]["StateId"].ToString().Equals("4"))
                    {
                        item.ForeColor = System.Drawing.Color.Gray;
                    }
                    listTables.Items.Add(item);
                }
            }
        }

        private void LoadCategory()
        {
            listCategory.Items.Clear();
            DataTable dtCategory = new DataTable();
            dtCategory = m_CategoryCtrl.GetAll();
            if (dtCategory.Rows.Count > 0)
            {
                for (int i = 0; i < dtCategory.Rows.Count; i++)
                {
                    
                    ListViewItem item = new ListViewItem(dtCategory.Rows[i]["CateName"].ToString(), 2);
                    ListViewItem.ListViewSubItem subId = new ListViewItem.ListViewSubItem(item, dtCategory.Rows[i]["CateId"].ToString());
                    item.SubItems.Add(subId);
                    listCategory.Items.Add(item);
                }
            }
        }

        private void LoadMenuByCate(String cateId)
        {
            listMenu.Items.Clear();
            DataTable dtTables = new DataTable();
            dtTables = m_MenuCtrl.GetByCate(cateId);
            Font f = new System.Drawing.Font("Tahoma", 12, FontStyle.Regular);
            Font fOut = new System.Drawing.Font("Tahoma", 12, FontStyle.Strikeout);
            if (dtTables.Rows.Count > 0)
            {
                for (int i = 0; i < dtTables.Rows.Count; i++)
                {
                    ListViewItem item = new ListViewItem(dtTables.Rows[i]["MenuName"].ToString(), 3);
                    if (dtTables.Rows[i]["State"].ToString().Equals("1"))
                    {
                        item.ForeColor = System.Drawing.Color.Blue;
                        item.Font = f;
                    }
                    else if (dtTables.Rows[i]["State"].ToString().Equals("0"))
                    {
                        item.ForeColor = System.Drawing.Color.Orange;
                        item.Font = fOut;
                    }
                    ListViewItem.ListViewSubItem subPrice = new ListViewItem.ListViewSubItem(item, dtTables.Rows[i]["Price"].ToString());
                    ListViewItem.ListViewSubItem subState = new ListViewItem.ListViewSubItem(item, dtTables.Rows[i]["State"].ToString());
                    if (subState.Text.Equals("1"))
                    {
                        subPrice.Font = f;
                        subPrice.ForeColor = System.Drawing.Color.Blue;
                        subState.Font = f;
                        subState.ForeColor = System.Drawing.Color.Blue;
                    } 
                    else
                    {
                        subPrice.Font = fOut;
                        subPrice.ForeColor = System.Drawing.Color.Orange;
                        subState.Font = fOut;
                        subState.ForeColor = System.Drawing.Color.Orange;
                    }
                    item.SubItems.Add(subPrice);
                    item.SubItems.Add(subState);
                    listMenu.Items.Add(item);
                }
            }
        }

        private void listArea_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection itemsChoise = listArea.SelectedItems;
            if (itemsChoise.Count > 0)
            {
                LoadTablesByAreaName(itemsChoise[0].Text);
                foreach(ListViewItem itemss in listArea.Items)
                {
                    Font f = new System.Drawing.Font("Tahoma", 8, FontStyle.Regular);
                    itemss.Font = f;
                }
                Font f2 = new System.Drawing.Font("Tahoma", 8, FontStyle.Bold);
                itemsChoise[0].Font = f2;
            }
        }

        private void listTables_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection itemsChoise = listTables.SelectedItems;
            foreach (ListViewItem itemss in listTables.Items)
            {
                Font f = new System.Drawing.Font("Tahoma", 8, FontStyle.Regular);
                if (itemss.ForeColor == System.Drawing.Color.Red)
                {
                    //Khong lam j
                }
                else
                {
                    itemss.Font = f;
                }
            }
            if (itemsChoise.Count > 0)
            {
                Font f2 = new System.Drawing.Font("Tahoma", 8, FontStyle.Bold);
                itemsChoise[0].Font = f2;
            }
        }

        private void listCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection itemsChoise = listCategory.SelectedItems;

            foreach (ListViewItem itemss in listCategory.Items)
            {
                Font f = new System.Drawing.Font("Tahoma", 8, FontStyle.Regular);
                itemss.Font = f;
            }
            if (itemsChoise.Count > 0)
            {
                ListViewItem itemSelected = itemsChoise[0];
                ListViewItem.ListViewSubItem subId = itemSelected.SubItems[1];
                LoadMenuByCate(subId.Text);
                Font f2 = new System.Drawing.Font("Tahoma", 8, FontStyle.Bold);
                itemsChoise[0].Font = f2;
            }
        }
    }
}
